package com.intelligent.health.base.controller;

import com.intelligent.health.base.utils.ValidateCodeUtils;
import entity.R;
import entity.ResultCodeEnum;
import entity.constant.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/26/0:40
 * @Description: 短信服务
 */
@Slf4j
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取登录短信验证码
     *
     * @param telePhone 手机号
     */
    @GetMapping("/getCode")
    public R getCode(@RequestParam("telePhone") String telePhone) {
        try {
            String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
            System.out.println(code);
            //发送短信验证码要钱
            //SmsUtils.sendShortMessage(telePhone, code);
            //redis 存储code
            redisTemplate.opsForValue().set(RedisConst.SENDTYPE_LOGIN + telePhone, code);
        } catch (Exception e) {
            log.error("短信发送失败: {}", telePhone);
            return R.setResult(ResultCodeEnum.SMS_SEND_ERROR);
        }
        log.info("短信发送成功: {}", telePhone);
        return R.setResult(ResultCodeEnum.SMS_SEND_SUCCESS);
    }

}
